
import tkinter as tk
from tkinter import ttk
import ttkbootstrap as ttkb
from tkinter import messagebox
class AppModbus(ttk.Frame):                                 # 继承自 tk.Tk
    def __init__(self,parent=None):
        super().__init__(parent) 
        self.place(x=0,y=36,height=410,width=530)  
        self.configure(bootstyle='')                         
        self.modbus()
    def modbus(self):    
        #tkinter变量------------------------------
        self.tag_port=tk.StringVar()                     #获取用户输入的端口号         
        self.tag_addrh=tk.StringVar()                    #获取用户输入的结束地址
        self.tag_addrl=tk.StringVar()                    #起始地址
        self.tag_code=tk.StringVar()                     #获取用户输入的功能码
        self.tag_rwaddr=tk.StringVar()                   #获取用户输入的读写地址
        self.tag_num=tk.StringVar()                      #获取用户输入的读写数量
        self.tag_display=tk.StringVar()
        self.tag_load=tk.IntVar()    
        #统一字体和大小----------------------------
        self.Font0='微软雅黑'
        self.FontSize=9
        #参数设置容器>>>>>>>>>>>>>>>>>>>>
        self.s=ttk.Style(self)
        self.s.configure('TLabelframe',font=(self.Font0))
        frame0=ttk.Labelframe(self,text='参数设置',style='TLabelframe')
        frame0.place(x=10,y=10,width=510,height=110)
        #输出容器>>>>>>>>>>>>>>>>>>>>>>>
        frame1=ttk.Labelframe(self,text='搜索显示',
                                # bootstyle='primary'
                                )
        frame1.place(x=10,y=130,width=510,height=265)
        #输出文本浏览------------------------
        self.Display=ttkb.ScrolledText(frame1,
                                        font=(self.Font0,self.FontSize)
                                        
                                        )
        self.Display.place(x=20,y=10,width=470,height=150)                      #20, 110, 47, 150
        #滚动条
        self.scroll=ttk.Scrollbar(frame1,bootstyle="dark-round")                        #滚动条
        self.scroll.place(x=473,y=10,width=17,height=150)  
        #控件关联
        self.scroll.config(command=self.Display.yview)
        self.Display.config(yscrollcommand=self.scroll.set)
        #进度条-------------------------------
        self.load0=ttk.Progressbar(frame1,bootstyle="success-striped")
        self.load0.place(x=20,y=165,width=470,height=15)   
        #搜索按钮-----------------------------
        bs=ttk.Style()
        bs.configure('my.TButton',font=(self.Font0,self.FontSize))
        #程序图标-----------------------------
        self.fun=ttk.Button(frame1,
                        text='开始搜索',
                        # command=thread0,
                        # style='my.TButton'

                        bootstyle="primary-outline"
                        )            #连接线程函数
        self.fun.place(x=20,y=190,width=225,height=40)   #20, 270, 470, 40
        self.stop=ttk.Button(frame1,
                        text='停止搜索',
                        # command=thread0,
                        # style='my.TButton'

                        bootstyle="danger-outline"
                        )            #连接线程函数
        self.stop.place(x=265,y=190,width=225,height=40)   #20, 270, 470, 40
        #文本显示-------------------------------
        #端口文本
        self.label_port=ttk.Label(frame0,text='串口号：',font=(self.Font0,self.FontSize))           
        self.label_port.place(x=15,y=10,width=50,height=25)                      #20, 10, 50, 20
        #结束地址文本
        self.label_addrh=ttk.Label(frame0,text='结束地址：',font=(self.Font0,self.FontSize))
        self.label_addrh.place(x=345,y=10,width=65,height=25)                    #180, 10, 60, 20
        #起始地址文本
        self.label_addrl=ttk.Label(frame0,text='起始地址：',font=(self.Font0,self.FontSize))
        self.label_addrl.place(x=175,y=10,width=65,height=25)                    #180, 10, 60, 20
        #功能码文本
        self.label_code=ttk.Label(frame0,text='功能码：',font=(self.Font0,self.FontSize))
        self.label_code.place(x=15,y=50,width=50,height=25)                      #20, 50, 50, 20      
        #读写地址文本
        self.label_rwaddr=ttk.Label(frame0,text='读取点位：',font=(self.Font0,self.FontSize))
        self.label_rwaddr.place(x=175,y=50,width=65,height=25)                   #180, 50, 60, 20
        #读取数量文本
        self.label_num=ttk.Label(frame0,text='读取数量：',font=(self.Font0,self.FontSize))
        self.label_num.place(x=345,y=50,width=65,height=25)                      #350, 50, 60, 20
        # #搜索显示
        # self.label_browser=ttk.Label(text='搜索显示：',font=(self.Font0,self.FontSize))
        # self.label_browser.place(x=20,y=90,width=65,height=25)                   #20, 90, 60, 20
        #文本框显示--------------------------------------------------------
        #端口号输入框
        self.in_port=ttk.Entry(frame0,textvariable=self.tag_port)
        self.in_port.place(x=65,y=10,width=80,height=25)                         #70, 10, 70, 20
        #结束地址号输入框
        self.in_addrh=ttk.Entry(frame0,textvariable=self.tag_addrh)
        self.in_addrh.place(x=410,y=10,width=80,height=25)                       #250, 10, 70, 20
        #起始地址号输入框
        self.in_addrl=ttk.Entry(frame0,textvariable=self.tag_addrl)
        self.in_addrl.place(x=240,y=10,width=80,height=25)                       #240, 10, 70, 20
        #功能码输入框
        self.in_code=ttk.Entry(frame0,textvariable=self.tag_code)
        self.in_code.place(x=65,y=50,width=80,height=25)                         #70, 50, 70, 20
        #读写地址输入框
        self.in_rwaddr=ttk.Entry(frame0,textvariable=self.tag_rwaddr)
        self.in_rwaddr.place(x=240,y=50,width=80,height=25)                      #250, 50, 70, 20
        #读写数量输入框
        self.in_num=ttk.Entry(frame0,textvariable=self.tag_num)
        self.in_num.place(x=410,y=50,width=80,height=25)                         #420, 50, 70, 20
class AppMQTT(ttk.Frame):                                   # 继承自 tk.Tk
    def __init__(self,parent=None):
        super().__init__(parent)                               # 有点相当于tk.Tk()
        self.place(x=0,y=36,height=410,width=530) 
        self.configure(bootstyle='') 
        self.mqtt()
    def mqtt(self): 
        #布局>>>>>>>>>>>>
        #tkinter变量------------------------------
        self.tag_port=tk.StringVar()                        #获取用户输入的端口    
        self.tag_ID=tk.StringVar()                          #获取用户输入的ID
        self.tag_addr=tk.StringVar()                        #获取用户输入的服务器地址
        self.tag_user=tk.StringVar()                        #获取用户输入的用户
        self.tag_password=tk.StringVar()                    #获取用户输入的密码
        self.tag_display=tk.StringVar()                     #消息显示
        self.tag_pubmessage=tk.StringVar()                  #要发布的消息
        # self.tag_load=tk.IntVar()    
        self.tag_topic=tk.StringVar()                       #主题
        #统一字体和大小----------------------------
        self.Font0='微软雅黑'
        self.FontSize=9
        #参数设置容器>>>>>>>>>>>>>>>>>>>>
        frame0=ttk.Labelframe(self,text='连接设置',bootstyle='inf1o')
        frame0.place(x=10,y=10,width=510,height=110)
        #输出容器>>>>>>>>>>>>>>>>>>>>>>>
        frame1=ttk.Labelframe(self,text='订阅发布',bootstyle='inf1o')
        frame1.place(x=10,y=130,width=510,height=265)
        #输出文本浏览------------------------
        self.Display=ttkb.ScrolledText(frame1,font=(self.Font0,self.FontSize))
        self.Display.place(x=20,y=5,width=285,height=230)                      #20, 110, 47, 150
        #发布消息文本框------------------------
        self.pubmessage=tk.Text(frame1,font=(self.Font0,self.FontSize))
        self.pubmessage.place(x=320,y=85,width=170,height=110)                      #20, 110, 47, 150
        #主题文本框
        self.in_topic=ttk.Entry(frame1,textvariable=self.tag_topic)
        self.in_topic.place(x=320,y=5,width=170,height=30)   
        #滚动条
        self.scroll=ttk.Scrollbar(frame1,bootstyle="dark-round")                        #滚动条
        self.scroll.place(x=288,y=5,width=17,height=230)  
        #控件关联
        self.scroll.config(command=self.Display.yview)
        self.Display.config(yscrollcommand=self.scroll.set)
        # #进度条-------------------------------
        # self.load0=ttk.Progressbar(frame1,bootstyle="success-striped")
        # self.load0.place(x=20,y=165,width=470,height=15)   
        #搜索按钮-----------------------------
        # s=ttk.Style()
        # s.configure('my.TButton',font=(Font0,20))
        #程序图标-----------------------------
        # self.fun=ttk.Button(frame1,
        #                 text='发布',
        #                 # command=thread0,
        #                 bootstyle="info-outline")            #连接线程函数
        # self.fun.place(x=20,y=190,width=235,height=40)   #20, 270, 470, 40
        #订阅按钮显示--------
        self.Ensub=ttk.Button(frame1,
                        text='订阅主题',
                        # command=thread0,
                        bootstyle="success-outline"
                        )            #连接线程函数
        self.Ensub.place(x=320,y=45,width=80,height=30)   #20, 270, 470, 40
        #取消订阅按钮显示--------
        self.UnEnsub=ttk.Button(frame1,
                        text='取消订阅',
                        # command=thread0,
                        bootstyle="info-outline")            #连接线程函数
        self.UnEnsub.place(x=410,y=45,width=80,height=30)   #20, 270, 470, 40
        #发布按钮显示--------
        self.pubb=ttk.Button(frame1,
                        text='发布消息',
                        # command=thread0,
                        bootstyle="info-outline")            #连接线程函数
        self.pubb.place(x=320,y=205,width=170,height=30)   #20, 270, 470, 40
        #文本显示-------------------------------
        #ID文本
        # self.label_port=ttk.Label(frame0,text='ID：',font=(self.Font0,self.FontSize))           
        # self.label_port.place(x=20,y=5,width=50,height=30)                      #20, 10, 50, 20
        #地址址文本
        self.label_addrh=ttk.Label(frame0,text='地址：',font=(self.Font0,self.FontSize))
        self.label_addrh.place(x=20,y=5,width=65,height=30)                    #180, 10, 60, 20
        #账户文本
        self.label_code=ttk.Label(frame0,text='账户：',font=(self.Font0,self.FontSize))
        self.label_code.place(x=20,y=45,width=50,height=30)                      #20, 50, 50, 20      
        #密码文本
        self.label_rwaddr=ttk.Label(frame0,text='密码：',font=(self.Font0,self.FontSize))
        self.label_rwaddr.place(x=200,y=45,width=65,height=30)                   #180, 50, 60, 20
        #端口文本
        self.label_rwaddr=ttk.Label(frame0,text='端口：',font=(self.Font0,self.FontSize))
        self.label_rwaddr.place(x=200,y=5,width=65,height=30)                   #180, 50, 60, 20
        # #读写数量文本
        # self.label_num=ttk.Label(frame0,text='读取数量：',font=(self.Font0,self.FontSize))
        # self.label_num.place(x=350,y=50,width=65,height=25)                      #350, 50, 60, 20
        # #搜索显示
        # self.label_browser=ttk.Label(text='搜索显示：',font=(self.Font0,self.FontSize))
        # self.label_browser.place(x=20,y=90,width=65,height=25)                   #20, 90, 60, 20
        #文本框显示--------------------------------------------------------
        #ID输入框
        # self.in_ID=ttk.Entry(frame0,textvariable=self.tag_ID)
        # self.in_ID.place(x=65,y=5,width=100,height=30)                         #70, 10, 70, 20
        #IP输入框
        self.in_addr=ttk.Entry(frame0,textvariable=self.tag_addr)
        self.in_addr.place(x=65,y=5,width=120,height=30)                       #250, 10, 70, 20
        #账户输入框
        self.in_user=ttk.Entry(frame0,textvariable=self.tag_user)
        self.in_user.place(x=65,y=45,width=120,height=30)                         #70, 50, 70, 20
        #密码输入框
        self.in_password=ttk.Entry(frame0,textvariable=self.tag_password,show='*')
        self.in_password.place(x=245,y=45,width=120,height=30)                      #250, 50, 70, 20
        #端口输入框
        self.in_port=ttk.Entry(frame0,textvariable=self.tag_port)
        self.in_port.place(x=245,y=5,width=120,height=30)                         #420, 50, 70, 20
        #连接按钮显示--------
        self.connect0=ttk.Button(frame0,
                        text='连接代理',
                        # command=thread0,
                        bootstyle="success-outline")            #连接线程函数
        self.connect0.place(x=385,y=45,width=100,height=30)   #20, 270, 470, 40
        # #状态按钮显示--------
        # self.info0=ttk.Button(frame0,
        #                 text='未连接',
        #                 # command=thread0,
        #                 bootstyle="secondary")            #连接线程函数
        # self.info0.place(x=390,y=45,width=100,height=30)   #20, 270, 470, 40
class AppRandom(ttk.Frame):
    def __init__(self,parent=None):
        super().__init__(parent)                               # 有点相当于tk.Tk()
        self.place(x=0,y=36,height=410,width=530) 
    pass        

class MainWindows(ttkb.Window):
    def __init__ (self):
        super().__init__()
        self.main()
        self.theme_num=0
        # self.page1() 
        # ttkb.Style("darkly")
        # cosmo morph
        ttkb.Style('morph')
        self.select_theme()
    def showinfo(self):
        messagebox.showinfo(title='Modbus通讯参数搜索',message='作者：工控万金油\n日期：2024年1月6日\n')
    def select_theme(self):
        # return
        self.var = ttkb.StringVar()
        self.theme_select_combo = ttk.Combobox(
                                textvariable=self.var,
                                values=['浅色主题','深色主题'],
                                bootstyle='primary')
        self.theme_select_combo.bind("<<ComboboxSelected>>", self.select_judge) # 绑定事件
        # self.theme_select_combo.insert(0,'浅色主题')
        self.theme_select_combo.place(x=440,y=5,width=80,height=30)   #20, 270, 470, 40
    def select_judge(self,event):
        tag_theme=self.theme_select_combo.get()
        if tag_theme == '浅色主题' :
            ttkb.Style('morph')
        elif tag_theme == '深色主题' :
            ttkb.Style('darkly')
        # print(f'主题选择触发--{tag_theme}')
        # ttkb.Style(self.themeSet)
        # ttkb.Style("morph")
    def select_theme_2(self,num):
        if num==1 :
            ttkb.Style('morph')
        if num==2 :
            ttkb.Style('darkly')
    def page1(self):
        self.fram0=AppModbus(self)
        self.page_b1.configure(bootstyle="primary")
        self.page_b2.configure(bootstyle="info-outline")
        # self.themeSet='morph'
        # self.theme0()
        # ttkb.Style("morph")

    def page2(self):
        self.fram1=AppMQTT(self)
        self.page_b2.configure(bootstyle="info")
        self.page_b1.configure(bootstyle="primary-outline")
        
        # self.themeSet='cyborg'
        # self.theme0()
        # ttkb.Style("cyborg")
    def page3(self):
        self.fram1=AppRandom(self)
        self.page_b2.configure(bootstyle="info-outline")
        self.page_b1.configure(bootstyle="primary-outline")
        # self.page_b3
    def about(self):
        # self.fram1=AppMQTT(self)
        self.showinfo()
        self.page_b2.configure(bootstyle="info-outline")
        self.page_b1.configure(bootstyle="primary-outline")
        # self.themeSet='cyborg'
        # self.theme0()
        # ttkb.Style("cyborg")
    def main(self):
        # s=ttk.Style()
        # s.configure(self,'mybutton',font=('黑体',12))
        self.page_b1=ttk.Button(self,
                        text='串口通讯搜索',
                        # style="mybutton")
                        bootstyle="primary-outline")       
        self.page_b1.place(x=10,y=5,width=100,height=30)   #20, 270, 470, 40
        #--mqtt---
        self.page_b2=ttk.Button(self,
                        text='MQTT客户端',
                        # command=thread0,
                        bootstyle="info-outline")            #连接线程函数
        self.page_b2.place(x=115,y=5,width=100,height=30)   #20, 270, 470, 40
        #--about---
        self.page_about=ttk.Button(self,
                        text='关于',
                        # command=thread0,
                        bootstyle="link")            #连接线程函数
        self.page_about.place(x=335,y=5,width=100,height=30)   #20, 270, 470, 40
        #------
        # self.page_b3=ttk.Button(self,
        #                 text='随机决策',
        #                 # command=thread0,
        #                 bootstyle="success-outline")            #连接线程函数
        # self.page_b3.place(x=220,y=5,width=100,height=30)   #20, 270, 470, 40
def print0():
    print('out!')
if __name__ == '__main__':
    root=MainWindows()
    wh=root.winfo_screenheight()
    ww=root.winfo_screenwidth()
    mwh=440
    mww=530
    x=(ww-mww)/2
    y=(wh-mwh)/2
    root.geometry("%dx%d+%d+%d"%(mww,mwh,x,y))                         #510,340
    root.title("集装箱v1.0")  
    root.page_b1.configure(command=root.page1)
    root.page_b2.configure(command=root.page2)
    root.page_about.configure(command=root.showinfo)
    # ttk.Style().configure('TButton',font='黑体 12')
    # ttk.Style().configure('TEntry',font='黑体 9')
    root.mainloop()